package com.aio.news.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;

import com.aio.news.R;

public class MainTabTextView extends TextView {

	private int normalImageId = -1;
	private int selectedImageId = -1;
	private boolean isSelected = false;

	public MainTabTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
	}

	public MainTabTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public MainTabTextView(Context context) {
		super(context);
	}

	void init(Context mContext, AttributeSet attrs) {
		TypedArray array = mContext.obtainStyledAttributes(attrs,
				R.styleable.MainTabTextViewAttribute);
		normalImageId = array.getResourceId(
				R.styleable.MainTabTextViewAttribute_normal_image, -1);
		selectedImageId = array.getResourceId(
				R.styleable.MainTabTextViewAttribute_selected_image, -1);
		isSelected = array.getBoolean(
				R.styleable.MainTabTextViewAttribute_isSelected, false);
		resetStates();
	}

	public void setSelectedImageId(int id) {
		this.selectedImageId = id;
	}

	public void setNormalImageId(int id) {
		this.normalImageId = id;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
		resetStates();
	}

	private void resetStates() {
		setCompoundDrawablesWithIntrinsicBounds(0, isSelected ? selectedImageId
				: normalImageId, 0, 0);
	}

}
